import { useMenusStore } from '@/stores/menus'
enum PrivilegeType
{ 
  View = 1001, // 查看
  Add = 1002, // 添加
  Edit = 1003,// 编辑
  Delete = 1004, // 删除
  Administer = 1005,// 管理
}
export default {
  auth(permission: PrivilegeType): boolean {
    const menusStore = useMenusStore()
    const paths = menusStore.currentPage.fullPath.trim().replace('/', '').split('/') 
    let show = <boolean>(false)
    let permissions = <Privilege[]>([])
    if(menusStore.menus) {
      menusStore.menus.forEach((one: Menu) => {
        if (paths.length > 0) {
          if (paths[0] === one.path) {
            if (paths.length == 1) {
              permissions = one.permissions
            } else {
              one.children?.forEach((two: Menu) => {
                if (paths[1] === two.path) {
                  if (paths.length == 2) {
                    permissions = two.permissions
                  } else {
                    two.children?.forEach((three: Menu) => {
                      if (paths[2] === three.path) {
                        permissions = three.permissions
                      }
                    })
                  }
                }
              })
            }
          }
        }
      })
    }
    permissions.forEach((res: Privilege) => {
      if (res.id === permission) show = true
    })
    return show
  },
  PrivilegeType
}